import React from 'react';
import { authority, addAuthority, parentAuthority, editAuthority } from '@/services/ant-design-pro/api';
import ListView from '@/components/Base/ListView'
import Helper from '@/components/Base/helper'

const MenuList: React.FC = () => {
  const columns = [
    Helper.Table('父亲id', 'parentId').val,
    Helper.Table('规则名称', 'authorityName').val,
  ];

  const formColumns = [
    Helper
      .Form('父级id', 'parentId')
      .Cascader(parentAuthority, { changeOnSelect: false })
      .Params({ page: 1 })
      .FieldNames({
        label: 'authorityName', value: 'authorityId'
      })
      .Width('sm')
      .val,
    Helper.Form('角色名称', 'authorityName')
      .FIP({
        rules: Helper.Rule().Require('不得为空').val,
      }).Width('md').val,
  ]

  const values = {
    headerTitle: '查询表格',
    ACTIONS: [
      {type: 'NEW', submit: (id, body) => {
        console.log('ud', id, body)
        if (id) {
          return editAuthority(id, body)
        }
        return addAuthority(body)
      }, beforeSubmit: (values: any) => ({...values, parentId: values.parentId && values.parentId[0]}) },
      {type: 'EDIT'}
    ],
    columns,
    request: authority,
    formColumns,
    initialValues: {
      parentId: [1],
      authorityName: '天神下凡'
    }
  }

  return (
    <ListView {...values} />
  );
};

export default MenuList;
